﻿#include <iostream>
#include <vector>
#include <string>
#include <mutex>
#include <curl/curl.h>
#include <filesystem>
#include <QtCore>
#include "Connection.h"
#include "SftpClient.h"

// -c=query -h=172.26.136.17 -P=22 -u=avery -p=3432 -r=/home/avery/sftp/ -f=.*
// -c=push -h=172.26.136.17 -P=22 -u=avery -p=3432 -r=/home/avery/sftp/ -l=E:/sftp/ -f=.* -t=1
// -c=pull -h=172.26.136.17 -P=22 -u=avery -p=3432 -r=/home/avery/sftp/ -l=E:/sftp/ -f=.* -t=1
int main(int argc, char* argv[])
{
	QCoreApplication app(argc, argv);

	QCommandLineParser parser;
	QCommandLineOption command("c"); //c：uploadfile、uploaddir、downfile、downloaddir、info
	command.setValueName("command");
	parser.addOption(command);

	QCommandLineOption hostname("h");
	hostname.setValueName("hostname");
	parser.addOption(hostname);

	QCommandLineOption hostport("P");
	hostport.setValueName("hostport");
	parser.addOption(hostport);

	QCommandLineOption username("u");
	username.setValueName("username");
	parser.addOption(username);

	QCommandLineOption password("p");
	password.setValueName("password");
	parser.addOption(password);

	QCommandLineOption remote("r");
	remote.setValueName("remote");
	parser.addOption(remote);

	QCommandLineOption local("l");
	local.setValueName("local");
	parser.addOption(local);

	QCommandLineOption filter("f");
	filter.setValueName("filter");
	parser.addOption(filter);

	QCommandLineOption threads("t");
	threads.setValueName("threads");
	parser.addOption(threads);

	parser.process(app);


	CURLM* multi_handle = curl_multi_init();
	if (!multi_handle)
	{
		std::cerr << "Failed to initialize curl multi handle" << std::endl;
		return 1;
	}

	int ts = parser.value(threads).toInt();
	if (ts <= 0)
	{
		ts = 5;
	}
	SftpClient client(multi_handle, ts);

	QStringList remotePathList;
	QString operation = parser.value("c");
	try
	{
		client.setLoginInfo(parser.value(hostname), parser.value(hostport), parser.value(username), parser.value(password));
		if (operation == "push")
		{
			client.uploadDir(parser.value(remote), parser.value(local), parser.value(filter));
		}
		else if (operation == "pull")
		{
			client.downloadDir(parser.value(remote), parser.value(local), parser.value(filter));
		}
		else if (operation == "query")
		{
			QQueue<QString> queue = client.exploreSftpFolderAndFiles(parser.value(remote), parser.value(filter));
			while (!queue.empty())
			{
				QString filePath = queue.front();
				qDebug() << filePath;
				queue.pop_front();
			}
		}
		else
		{
			std::cout << "Error: Invalid argument." << std::endl;
			curl_multi_cleanup(multi_handle);
			return EXIT_FAILURE;
		}
	}
	catch (const std::exception& e)
	{
		std::cerr << "Error: " << e.what() << std::endl;
		curl_multi_cleanup(multi_handle);
		return 1;
	}

	int still_running = 1;
	curl_multi_perform(multi_handle, &still_running);
	while (still_running)
	{
		int numfds = 0;
		curl_multi_wait(multi_handle, nullptr, 0, 1000, &numfds);
		curl_multi_perform(multi_handle, &still_running);
	}

	curl_multi_cleanup(multi_handle);

	std::cout << "All transfers completed" << std::endl;
	//return app.exec();
}
